﻿using System;
using System.Collections.Generic;
using Quicklight.Web.Injection;

namespace Quicklight.Web.Events {
   
   public class EventManager {
      private Dictionary<string, object> eventDistributers;

      public EventManager() {
         eventDistributers = new Dictionary<string, object>();
      }

      public static EventManager DefaultManager {
         get { return GetManager(""); }
      }

      public static EventManager GetManager(string name) {
         if (!ObjectManager.Instance.IsInstanceRegistered<EventManager>(name))
            ObjectManager.Instance.RegisterInstance<EventManager>(name, new EventManager());

         return ObjectManager.Instance.Resolve<EventManager>(name);
      }

      public T GetEvent<T>() where T : EventDistributer<T> {
         if (!eventDistributers.ContainsKey(typeof(T).FullName)) {
            T eventDistributer = (T)Activator.CreateInstance(typeof(T));
            eventDistributers.Add(typeof(T).FullName, eventDistributer);
         }

         return (T)eventDistributers[typeof(T).FullName];
      }
   }
}
